package com.example.practica1;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import org.xmlpull.v1.XmlSerializer;

import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.util.Xml;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Activity3 extends Activity {

	int maquina = 0;
	int jugador = 0;
	String name1 = "";

	static int cont1 = 0;
	static int cont2 = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_activity3);

		Bundle b = this.getIntent().getExtras();
		maquina = b.getInt("Maquina");
		name1 = b.getString("NombreJug1").toString();

		System.out.println("VALOR DE LA MAQUINA " + maquina);

		ImageView resMaq = (ImageView) findViewById(R.id.ResMaq);

		TextView tv2 = (TextView) findViewById(R.id.Cont2);
		TextView tv1 = (TextView) findViewById(R.id.Cont1);
		tv1.setText("Victorias " + name1 + " " + cont1);
		tv2.setText("Victorias Máquina " + cont2);

		switch (maquina) {
		case 1:
			resMaq.setImageDrawable(getResources().getDrawable(
					R.drawable.piedra));
			resMaq.setVisibility(1);

			break;

		case 2:

			resMaq.setImageDrawable(getResources()
					.getDrawable(R.drawable.papel));
			resMaq.setVisibility(1);

			break;
		case 3:
			resMaq.setImageDrawable(getResources().getDrawable(
					R.drawable.tijeras));
			resMaq.setVisibility(1);

			break;
		case 4:

			resMaq.setImageDrawable(getResources().getDrawable(
					R.drawable.lagarto));
			resMaq.setVisibility(1);

			break;
		case 5:

			resMaq.setImageDrawable(getResources().getDrawable(R.drawable.spok));
			resMaq.setVisibility(1);

			break;

		}

		Bundle b2 = this.getIntent().getExtras();
		jugador = b2.getInt("Jugador");
		name1 = b.getString("NombreJug1").toString();

		System.out.println("VALOR DEL JUGADOR " + jugador);

		ImageView resJug = (ImageView) findViewById(R.id.ResJug1);
		TextView NombreJug = (TextView) findViewById(R.id.Jug1);
		NombreJug.setText(name1);

		switch (jugador) {
		case 1:
			resJug.setImageDrawable(getResources().getDrawable(
					R.drawable.piedra));
			resJug.setVisibility(1);

			break;

		case 2:

			resJug.setImageDrawable(getResources()
					.getDrawable(R.drawable.papel));
			resJug.setVisibility(1);

			break;
		case 3:
			resJug.setImageDrawable(getResources().getDrawable(
					R.drawable.tijeras));
			resJug.setVisibility(1);

			break;
		case 4:

			resJug.setImageDrawable(getResources().getDrawable(
					R.drawable.lagarto));
			resJug.setVisibility(1);

			break;
		case 5:

			resJug.setImageDrawable(getResources().getDrawable(R.drawable.spok));
			resJug.setVisibility(1);

			break;

		}

		TextView vr = (TextView) findViewById(R.id.VistaResultado);

		int gana = 0;

		if (jugador == 1 && maquina == 3)
			gana = 1;
		if (jugador == 1 && maquina == 4)
			gana = 1;
		if (jugador == 2 && maquina == 5)
			gana = 1;
		if (jugador == 2 && maquina == 1)
			gana = 1;
		if (jugador == 3 && maquina == 4)
			gana = 1;
		if (jugador == 3 && maquina == 2)
			gana = 1;
		if (jugador == 4 && maquina == 2)
			gana = 1;
		if (jugador == 4 && maquina == 5)
			gana = 1;
		if (jugador == 5 && maquina == 1)
			gana = 1;
		if (jugador == 5 && maquina == 3)
			gana = 1;
		if (jugador == maquina)
			gana = 2;

		switch (gana) {
		case 0:
			vr.setText("Gana la máquina");
			cont2 = cont2 + 1;
			tv2.setText("Victorias de la Máquina " + cont2);
			break;

		case 1:
			vr.setText("Gana " + name1);
			cont1 = cont1 + 1;
			tv1.setText("Victorias " + name1 + " " + cont1);
			break;
		case 2:
			vr.setText("Empate");
			break;
		}

		Button volverJugar = (Button) findViewById(R.id.VolverAJugar);

		volverJugar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Activity3.this, Activity1.class);
				intent.putExtra("NombreJug1", name1);
				startActivity(intent);
			}

		});

		Button reinicio = (Button) findViewById(R.id.Reiniciar);

		reinicio.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				escribirXML();
				cont1 = 0;
				cont2 = 0;
				finish();
			}

		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity3, menu);
		return true;
	}

	@Override
	public void onBackPressed() {
		Toast toast = Toast.makeText(this, "TRAMPOSO!!!", Toast.LENGTH_SHORT);

		toast.show();

		ToneGenerator sound = new ToneGenerator(AudioManager.STREAM_SYSTEM, 100);
		sound.startTone(ToneGenerator.TONE_PROP_BEEP);

		Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

		vibrator.vibrate(500);
	}

	@Override
	protected void onRestart() {
		finish();
		super.onRestart();
	}
	
	private void escribirXML() {
		FileOutputStream fout = null;

		try {
			fout = openFileOutput("ranking.xml",MODE_PRIVATE);
		} catch (FileNotFoundException e) {
			Toast.makeText(getApplicationContext(), e.getMessage(),
					Toast.LENGTH_LONG).show();
		}

		XmlSerializer serializer = Xml.newSerializer();
		try {
			serializer.setOutput(fout, "UTF-8");
			
			serializer.startDocument(null,true);
			serializer.setFeature(
					"http://xmlpull.org/v1/doc/features.html#indent-output",
					true);

			serializer.startTag(null, "ranking");

			serializer.startTag(null, "jugador");
			serializer.text(name1);
			serializer.endTag(null, "jugador");
			serializer.startTag(null, "victorias");
			serializer.text(""+cont1);
			serializer.endTag(null, "victorias");
			
			serializer.startTag(null, "jugador");
			serializer.text("Máquina");
			serializer.endTag(null, "jugador");
			serializer.startTag(null, "victorias");
			serializer.text(""+cont2);
			serializer.endTag(null, "victorias");

			
			serializer.endTag(null, "ranking");

			serializer.endDocument();
			serializer.flush();
			fout.close();
			
			
			Toast.makeText(getApplicationContext(), "Fichero escrito correctamente",
					Toast.LENGTH_LONG).show();
		} catch (Exception e) {
			Toast.makeText(getApplicationContext(), e.getMessage(),
					Toast.LENGTH_LONG).show();
		}
	}
	
	
}
